<template>
  <div>
    <!-- 面包屑 -->
    <div class="crumbs">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-s-fold"></i> 首页</el-breadcrumb-item>
        <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <!-- 表单 -->
    <el-card shadow="always">
      <!-- 表头 -->
      <div slot="header">
        <el-form label-width="100px">
          <el-form-item label="用户名:">
            <el-input v-model="username" placeholder="请输入用户名" size="medium" style="width: 350px;"></el-input>
          </el-form-item>
          <el-form-item label="用户电话:">
            <el-input v-model="phone" placeholder="请输入用户电话" size="medium" style="width: 350px;"></el-input>
          </el-form-item>
          <el-form-item label="订单时间:" size="normal">
            <div class="block">
              <el-date-picker value-format="yyyy-MM-dd HH:mm" v-model="timeArr" type="daterange" 
                range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期">
              </el-date-picker>
            </div>
          </el-form-item>
        </el-form>
        <el-button type="primary" size="medium" @click="getSearchUserList">查询</el-button>
        <el-button size="medium" @click="clearForm">取消</el-button>
      </div>
      <!-- 表体 -->
      <!-- 表格 -->
      <el-table :data="tableData" v-loading="loading" border style="width: 100%" size="medium">
        <el-table-column prop="id" label="用户ID">
        </el-table-column>
        <el-table-column prop="username" label="用户名">
        </el-table-column>
        <el-table-column prop="phone" label="手机号">
        </el-table-column>
        <el-table-column prop="createdate" label="注册时间">
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination style="text-align:center;margin-top: 20px;" @current-change="getUserList" @size-change="getUserList"
        :current-page.sync="pageInfo.pageNum" :page-sizes="[3, 5, 10, 20]" :page-size.sync="pageInfo.pageSize"
        layout="prev, pager, next,total,jumper,->, sizes" :total="pageInfo.total" :pager-count="7">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "UserManager",
  data() {
    return {
      pageInfo: {
        pageNum: 1,
        pageSize: 3,
        total: 20,
      },
      username: '',
      phone: '',
      timeArr: [],
      tableData: [],
      loading: false,
    }
  },
  computed: {
    starttime() {
      if(this.timeArr.length > 0) {
        return this.timeArr[0]
      }else {
        return '';
      }
    },
    endtime() {
      if(this.timeArr.length > 0) {
        return this.timeArr[1]
      }else {
        return '';
      }
    },
  },
  mounted() {
    this.getUserList();
  },
  methods: {
    async getUserList() {
      this.loading = true;
      let { pageNum, pageSize } = this.pageInfo;
      let { username, phone, starttime, endtime } = this;
      let query = this.$QS.stringify({ pageNum, pageSize, username, phone, starttime, endtime });
      let res = await this.$API.UserManager.reqUserList(query);
      if (res.code == 200) {
        this.pageInfo.total = res.data.total;
        this.tableData = res.data.list;
      } else {
        this.tableData = [];
        this.pageInfo.total = 0;
        this.$message({message: '没有数据',type: 'error'})
      }
      this.loading = false;
    },
    async getSearchUserList() {
      this.pageInfo.pageNum = 1;
      await this.getUserList();
      if(this.tableData.length > 0) {
        this.$message({message: '查询成功',type: 'success'})
      }
    },
    clearForm(){
      this.username = '';
      this.phone = '';
      this.timeArr = [];
      this.pageInfo.pageNum = 1;
      this.getUserList();
    }
  },
  watch: {
    timeArr(){
      if(!this.timeArr){
        this.timeArr = [];
      }
    },
  }
}
</script>

<style scoped></style>